package cn.mdmm.user.service.impl.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;

import cn.mdmm.user.entity.news.NewsInfoModel;
import cn.mdmm.user.entity.response.ResponseBodyToList;
import cn.mdmm.user.entity.type.TypeInfoModel;
import cn.mdmm.user.entity.user.UserCollectModel;
import cn.mdmm.user.entity.user.request.UserCollectRequest;
import cn.mdmm.user.mapper.news.NewsInfoMapper;
import cn.mdmm.user.mapper.user.UserCollectMapper;
import cn.mdmm.user.service.type.TypeInfoService;
import cn.mdmm.user.service.user.UserCollectService;
import cn.modoumama.common.exception.RequiredException;
import cn.modoumama.common.response.Response;
import cn.modoumama.service.base.impl.BaseServiceImpl;


/**
* 类名称：UserCollect对象定义<br>
* 类描述：业务实现类<br>
* 类生产方式：工具自动生成代码<br>
* 创建人：dengqiang<br>
* 创建时间：2017-06-30 13:22:11<br>
* 修改人：  <br>
* 修改时间：<br>
* 修改备注：<br>
* @version   V1.0
*/

@Service(version = "1.0")
public class UserCollectServiceImpl extends BaseServiceImpl<UserCollectModel,Integer> implements UserCollectService{
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	UserCollectMapper userCollectMapper;	
	@Autowired
	private TypeInfoService typeInfoService;
	
	@Autowired
	private NewsInfoMapper newsInfoMapper;
	
	@Autowired
	public void setMapper(UserCollectMapper genericMapper) {
		super.setGenericMapper(genericMapper);
	}

	//------------------请在此添加自定义方法（开始）------------------


	@Override
	public Response collect(UserCollectRequest request) {
		Response response = new Response();
		if(request.getCollectId() == null){
			throw new RequiredException("collectId");
		}
		UserCollectModel userCollectModel = new UserCollectModel();
		userCollectModel.setUserId(request.getUserId());
		userCollectModel.setCollectId(request.getCollectId());
		
		userCollectModel = findObjectByModel(userCollectModel);
		
		//有收藏就不再收藏
		if(userCollectModel == null){
			userCollectModel = new UserCollectModel();
			userCollectModel.setUserId(request.getUserId());
			userCollectModel.setCollectId(request.getCollectId());
			userCollectModel.setCreateTime(new Date());
			insert(userCollectModel);
			//添加商品收藏量
			newsInfoMapper.addColletNum(request.getCollectId());
		}
		
		return response;
	}

	@Override
	public Response getCollectList(UserCollectRequest request) {
		ResponseBodyToList<NewsInfoModel> response = new ResponseBodyToList<>();
		if(request.getType() == null){
			throw new RequiredException("type");
		}
		TypeInfoModel typeInfo = typeInfoService.getTypeById(request.getType());
		List<Integer> typeChildrenIds = typeInfo.getChildrenIds();
		String type = request.getType()+"";
		for (Integer integer : typeChildrenIds) {
			type = type + "," + integer;
		}
		
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("userId", request.getUserId());
		paramMap.put("newsType", type);
		List<NewsInfoModel> goodsList  = userCollectMapper.getCollectGoods(paramMap);
		response.setData(goodsList);
		return response;
	}
	//------------------请在此添加自定义方法（结束）------------------
}
